|
|
@@ -0,0 +1,171 @@
|
|
1
|
+# -- coding: utf-8 --
|
|
2
|
+"""simditor widgets."""
|
|
3
|
+from __future__ import absolute_import
|
|
4
|
+
|
|
5
|
+from django import forms
|
|
6
|
+from django.conf import settings
|
|
7
|
+from django.core.exceptions import ImproperlyConfigured
|
|
8
|
+from django.core.serializers.json import DjangoJSONEncoder
|
|
9
|
+from django.template.loader import render_to_string
|
|
10
|
+from django.utils.encoding import force_text
|
|
11
|
+from django.utils.functional import Promise
|
|
12
|
+from django.utils.html import conditional_escape
|
|
13
|
+from django.utils.safestring import mark_safe
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+try:
|
|
17
|
+ # Django >=2.1
|
|
18
|
+ from django.forms.widgets import get_default_renderer
|
|
19
|
+ IS_NEW_WIDGET = True
|
|
20
|
+except ImportError:
|
|
21
|
+ IS_NEW_WIDGET = False
|
|
22
|
+
|
|
23
|
+try:
|
|
24
|
+ # Django >=1.7
|
|
25
|
+ from django.forms.utils import flatatt
|
|
26
|
+except ImportError:
|
|
27
|
+ # Django <1.7
|
|
28
|
+ from django.forms.util import flatatt # pylint disable=E0611, E0401
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+class LazyEncoder(DjangoJSONEncoder):
|
|
32
|
+ """LazyEncoder."""
|
|
33
|
+
|
|
34
|
+ # pylint disable=E0202
|
|
35
|
+ def default(self, obj):
|
|
36
|
+ if isinstance(obj, Promise):
|
|
37
|
+ return force_text(obj)
|
|
38
|
+ return super(LazyEncoder, self).default(obj)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+JSON_ENCODE = LazyEncoder().encode
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+FULL_TOOLBAR = [
|
|
45
|
+ 'title', 'bold', 'italic', 'underline', 'strikethrough', 'fontScale',
|
|
46
|
+ 'color', '|', 'ol', 'ul', 'blockquote', 'code', 'table', '|', 'link',
|
|
47
|
+ 'image', 'hr', '|', 'indent', 'outdent', 'alignment', 'checklist',
|
|
48
|
+ 'markdown', 'fullscreen'
|
|
49
|
+]
|
|
50
|
+
|
|
51
|
+DEFAULT_TOOLBAR = [
|
|
52
|
+ 'title', 'bold', 'italic', 'underline', 'strikethrough', 'fontScale',
|
|
53
|
+ 'color', '|', 'ol', 'ul', 'blockquote', 'table', '|', 'link',
|
|
54
|
+ 'image', 'hr', '|', 'indent', 'outdent', 'alignment'
|
|
55
|
+]
|
|
56
|
+
|
|
57
|
+DEFAULT_CONFIG = {
|
|
58
|
+ 'toolbar': DEFAULT_TOOLBAR,
|
|
59
|
+ 'cleanPaste': True,
|
|
60
|
+ 'tabIndent': True,
|
|
61
|
+ 'pasteImage': True,
|
|
62
|
+ 'upload': {
|
|
63
|
+ 'url': '/',
|
|
64
|
+ 'fileKey': 'file'
|
|
65
|
+ }
|
|
66
|
+}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+class SimditorWidget(forms.Textarea):
|
|
70
|
+ """
|
|
71
|
+ Widget providing Simditor for Rich Text Editing.abs
|
|
72
|
+ Supports direct image uploads and embed.
|
|
73
|
+ """
|
|
74
|
+ class Media:
|
|
75
|
+ """Media."""
|
|
76
|
+
|
|
77
|
+ css_list = [
|
|
78
|
+ 'simditor/styles/simditor.min.css'
|
|
79
|
+ ]
|
|
80
|
+
|
|
81
|
+ if 'emoji' in settings.SIMDITOR_TOOLBAR:
|
|
82
|
+ css_list.append('simditor/styles/simditor-emoji.css')
|
|
83
|
+
|
|
84
|
+ if 'fullscreen' in settings.SIMDITOR_TOOLBAR:
|
|
85
|
+ css_list.append('simditor/styles/simditor-fullscreen.min.css')
|
|
86
|
+
|
|
87
|
+ if 'checklist' in settings.SIMDITOR_TOOLBAR:
|
|
88
|
+ css_list.append('simditor/styles/simditor-checklist.min.css')
|
|
89
|
+
|
|
90
|
+ if 'markdown' in settings.SIMDITOR_TOOLBAR:
|
|
91
|
+ css_list.append('simditor/styles/simditor-markdown.min.css')
|
|
92
|
+
|
|
93
|
+ css = {'all': tuple(settings.STATIC_URL + url for url in css_list)}
|
|
94
|
+
|
|
95
|
+ jquery_list = ['simditor/scripts/jquery.min.js',
|
|
96
|
+ 'simditor/scripts/module.min.js',
|
|
97
|
+ 'simditor/scripts/hotkeys.min.js',
|
|
98
|
+ 'simditor/scripts/uploader.min.js',
|
|
99
|
+ 'simditor/scripts/simditor.min.js']
|
|
100
|
+
|
|
101
|
+ if 'fullscreen' in settings.SIMDITOR_TOOLBAR:
|
|
102
|
+ jquery_list.append('simditor/scripts/simditor-fullscreen.min.js')
|
|
103
|
+
|
|
104
|
+ if 'checklist' in settings.SIMDITOR_TOOLBAR:
|
|
105
|
+ jquery_list.append('simditor/scripts/simditor-checklist.min.js')
|
|
106
|
+
|
|
107
|
+ if 'markdown' in settings.SIMDITOR_TOOLBAR:
|
|
108
|
+ jquery_list.append('simditor/scripts/marked.min.js')
|
|
109
|
+ jquery_list.append('simditor/scripts/to-markdown.min.js')
|
|
110
|
+ jquery_list.append('simditor/scripts/simditor-markdown.min.js')
|
|
111
|
+
|
|
112
|
+ if 'image' in settings.SIMDITOR_TOOLBAR:
|
|
113
|
+ jquery_list.append('simditor/scripts/simditor-dropzone.min.js')
|
|
114
|
+
|
|
115
|
+ if 'emoji' in settings.SIMDITOR_TOOLBAR:
|
|
116
|
+ jquery_list.append('simditor/scripts/simditor-emoji.js')
|
|
117
|
+
|
|
118
|
+ js = tuple(settings.STATIC_URL + url for url in jquery_list)
|
|
119
|
+
|
|
120
|
+ try:
|
|
121
|
+
|
|
122
|
+ js += (settings.STATIC_URL + 'simditor/simditor-init.js',)
|
|
123
|
+ except AttributeError:
|
|
124
|
+ raise ImproperlyConfigured("django-simditor requires \
|
|
125
|
+ SIMDITOR_MEDIA_PREFIX setting. This setting specifies a \
|
|
126
|
+ URL prefix to the ckeditor JS and CSS media (not \
|
|
127
|
+ uploaded media). Make sure to use a trailing slash: \
|
|
128
|
+ SIMDITOR_MEDIA_PREFIX = '/media/simditor/'")
|
|
129
|
+
|
|
130
|
+ def __init__(self, *args, **kwargs):
|
|
131
|
+ super(SimditorWidget, self).__init__(*args, **kwargs)
|
|
132
|
+ # Setup config from defaults.
|
|
133
|
+ self.config = DEFAULT_CONFIG.copy()
|
|
134
|
+
|
|
135
|
+ # Try to get valid config from settings.
|
|
136
|
+ configs = getattr(settings, 'SIMDITOR_CONFIGS', None)
|
|
137
|
+ if configs:
|
|
138
|
+ if isinstance(configs, dict):
|
|
139
|
+ self.config.update(configs)
|
|
140
|
+ else:
|
|
141
|
+ raise ImproperlyConfigured(
|
|
142
|
+ 'SIMDITOR_CONFIGS setting must be a dictionary type.')
|
|
143
|
+
|
|
144
|
+ def build_attrs(self, base_attrs, extra_attrs=None, **kwargs):
|
|
145
|
+ """
|
|
146
|
+ Helper function for building an attribute dictionary.
|
|
147
|
+ This is combination of the same method from Django<=1.10 and Django1.11
|
|
148
|
+ """
|
|
149
|
+ attrs = dict(base_attrs, **kwargs)
|
|
150
|
+ if extra_attrs:
|
|
151
|
+ attrs.update(extra_attrs)
|
|
152
|
+ return attrs
|
|
153
|
+
|
|
154
|
+ def render(self, name, value, attrs=None, renderer=None):
|
|
155
|
+ if value is None:
|
|
156
|
+ value = ''
|
|
157
|
+ final_attrs = self.build_attrs(self.attrs, attrs, name=name)
|
|
158
|
+
|
|
159
|
+ params = ('simditor/widget.html', {
|
|
160
|
+ 'final_attrs': flatatt(final_attrs),
|
|
161
|
+ 'value': conditional_escape(force_text(value)),
|
|
162
|
+ 'id': final_attrs['id'],
|
|
163
|
+ 'config': JSON_ENCODE(self.config)
|
|
164
|
+ })
|
|
165
|
+
|
|
166
|
+ if renderer is None and IS_NEW_WIDGET:
|
|
167
|
+ renderer = get_default_renderer()
|
|
168
|
+
|
|
169
|
+ data = renderer.render(*params) if IS_NEW_WIDGET else render_to_string(*params)
|
|
170
|
+
|
|
171
|
+ return mark_safe(data)
|